#include<jsoncpp/json/json.h>
#include<signal.h>
#include"httplib.h"
#include"db.hpp"

const char* CONTENT_TYPE = "application/json";
MYSQL* mysql = NULL;

int main(){
	using namespace httplib;

	mysql = order_system::MySQLInit();
	signal(SIGINT,[](int){order_system::MySQLRelease(mysql);exit(0);});
	order_system::DishTable dish_table(mysql);
	order_system::OrderTable order_table(mysql);

	Server server;
	server.Post("/dish",[&dish_table](const Request& req,Response& resp){
		Json::Value req_json;
		Json::Value resp_json;
		Json::Reader reader;
		Json::FastWriter writer;
		
	
		bool ret = reader.parse(req.body,req_json);
		if(!ret){
			printf("paese body failed!");
			resp_json["OK"]=false;
			resp_json["reson"]="paese body failed!";
			resp.status=400;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
		if(req_json["name"].empty()||req_json["price"].empty()){
			printf("格式错误!\n");
			resp_json["OK"]=false;
			resp_json["reason"]="格式错误!\n";
			resp.status=400;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
	
		ret = dish_table.Insert(req_json);
		if(!ret){
			printf("菜品信息数据库插入失败\n!");
			resp_json["OK"]=false;
			resp_json["reason"]="菜品信息数据库插入失败\n";
			resp.status=500;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
		printf("菜品信息数据库插入成功!\n");
		resp_json["ok"]=true;
		resp.status=200;
		resp.set_content(writer.write(resp_json),CONTENT_TYPE);
		return;
	});
	server.Get("/dish",[&dish_table](const Request& req,Response& resp){
		(void) req;
		Json::Value resp_json;
		Json::FastWriter writer;
		printf("获取所有菜品信息!\n");
		bool ret = dish_table.SelectAll(&resp_json);
		if(!ret){
			printf("查看所有菜品失败！\n");
			resp_json["OK"]=false;
			resp_json["reason"]="查看所有菜品失败!\n";
			resp.status=500;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}

		resp.status = 200;
		resp.set_content(writer.write(resp_json),CONTENT_TYPE);
		return;
	});
	server.Get(R"(/dish/(\d+))",[&dish_table](const Request& req,Response& resp){
		Json::Value resp_json;
		Json::FastWriter writer;
		
		int32_t dish_id = std::stoi(req.matches[1]);
		printf("获取到编号为%d的菜品\n",dish_id);
		
		bool ret = dish_table.SelectOne(dish_id,&resp_json);
		if(!ret){
			printf("菜品信息查看失败！\n");
			resp_json["OK"]=false;
			resp_json["reason"]="菜品信息查看失败!\n";
			resp.status=500;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
		
		resp.status = 200;
		resp.set_content(writer.write(resp_json),CONTENT_TYPE);
		return;
	});
	server.Put(R"(/dish(\d+))",[&dish_table](const Request& req,Response& resp){
		Json::Value req_json;
		Json::Value resp_json;
		Json::Reader reader;
		Json::FastWriter writer;
		
		int32_t dish_id = std::stoi(req.matches[1]);
		printf("修改编号为%d的菜品!\n",dish_id);
		

		bool ret = reader.parse(req.body,req_json);
		if(!ret){
			printf("paese body failed!");
			resp_json["OK"]=false;
			resp_json["reson"]="paese body failed!";
			resp.status=400;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
		
		if(req_json["name"].empty()||req_json["price"].empty()){
			printf("格式错误!\n");
			resp_json["OK"]=false;
			resp_json["reason"]="格式错误!\n";
			resp.status=400;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
	
		req_json["dish_id"] = dish_id;	
		ret = dish_table.Update(req_json);
		if(!ret){
			printf("菜品信息数据库修改失败\n!");
			resp_json["OK"]=false;
			resp_json["reason"]="菜品信息数据库修改失败\n";
			resp.status=500;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
		
		resp_json["ok"]=true;
		resp.status=200;
		resp.set_content(writer.write(resp_json),CONTENT_TYPE);
		return;
	});
	server.Delete(R"(/dish/(\d+))",[&dish_table](const Request& req,Response& resp){
		Json::Value resp_json;
		Json::FastWriter writer;
		
		int32_t dish_id = std::stoi(req.matches[1]);
		printf("删除编号为%d的菜品!\n",dish_id);
		
		bool ret = dish_table.Delete(dish_id);
		if(!ret){
			printf("删除菜品信息数据库操作失败\n!");
			resp_json["OK"]=false;
			resp_json["reason"]="删除菜品信息数据库操作失败\n";
			resp.status=500;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}

		resp_json["ok"]=true;
		resp.status=200;
		resp.set_content(writer.write(resp_json),CONTENT_TYPE);
		return;
	});
	
	
	server.Post("/order",[&order_table](const Request& req,Response& resp){
		Json::Value req_json;
		Json::Value resp_json;
		Json::Reader reader;
		Json::FastWriter writer;
		printf("新增一个订单\n");
		
		bool ret = reader.parse(req.body,req_json);
		if(!ret){
			printf("未获取订单数据！\n");
			resp_json["OK"]=false;
			resp_json["reason"]="未获取订单数据！\n";
			resp.status=400;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
		
		if(req_json["table_id"].empty()||req_json["time"].empty()||req_json["dish_ids"].empty()){
			printf("插入订单数据格式错误！\n");
			resp_json["OK"]=false;
			resp_json["reason"]="插入订单数据格式错误\n";
			resp.status=400;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}

		req_json["status"]=1;
		
		const Json::Value dish_ids = req_json["dish_ids"];
		req_json["dishes"]=writer.write(dish_ids);
		
		ret = order_table.Insert(req_json);
		if(!ret){
			printf("插入订单数据失败！\n");
			resp_json["OK"]=false;
			resp_json["reason"]="插入订单数据失败\n";
			resp.status=500;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
		
		resp_json["ok"]=true;
		resp.status=200;
		resp.set_content(writer.write(resp_json),CONTENT_TYPE);
		return;
	});
	server.Get("/order",[&order_table](const Request& req,Response& resp){
		printf("获取所有订单信息：");
		Json::Value resp_json;
		Json::FastWriter writer;
		
		
		bool ret = order_table.SelectAll(&resp_json);
		if(!ret){
			printf("查看所有订单失败！\n");
			resp_json["OK"]=false;
			resp_json["reason"]="查看所有订单失败!\n";
			resp.status=500;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
		
		resp_json["ok"]=true;
		resp.status = 200;
		resp.set_content(writer.write(resp_json),CONTENT_TYPE);
		return;
	});
	server.Put(R"(/order/(\d+))",[&order_table](const Request& req,Response& resp){
		Json::Value req_json;
		Json::Value resp_json;
		Json::Reader reader;
		Json::FastWriter writer;
		
		int32_t order_id = std::stoi(req.matches[1]);
		printf("修改编号为%d的订单状态",order_id);	
		
		bool ret = reader.parse(req.body,req_json);
		if(!ret){
			printf("提取订单状态信息失败！\n");
			resp_json["OK"]=false;
			resp_json["reason"]="提取订单状态信息失败!\n";
			resp.status=500;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
		
		if(req_json["table_id"].empty()||req_json["time"].empty()||req_json["dish_ids"].empty()){
			printf("插入订单数据格式错误！\n");
			resp_json["OK"]=false;
			resp_json["reason"]="订单数据格式错误\n";
			resp.status=400;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
		
		req_json["order_id"] = order_id;
		ret = order_table.ChangeState(req_json);
		if(!ret){
			printf("改变订单状态信息失败！\n");
			resp_json["OK"]=false;
			resp_json["reason"]="改变订单状态信息失败!\n";
			resp.status=500;
			resp.set_content(writer.write(resp_json),CONTENT_TYPE);
			return;
		}
		
		resp_json["ok"]=true;
		resp.status = 200;
		resp.set_content(writer.write(resp_json),CONTENT_TYPE);
		return;
	});
	
	
	
	server.set_base_dir("./wwwroot");
	
	
	
	server.listen("0.0.0.0",9094);
	return 0;
}